/*
 * support3
 */
package com.pitop.scene.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.scene.entity.Scene;
import com.pitop.scene.service.SceneSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场景 控制器
 *
 * @author support
 */
@RestController
@RequestMapping("/scene")
@Slf4j
@Api(value = "场景控制器", description = "场景控制器")
public class SceneCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private SceneSV sceneSV;

    /**
     * 查询Scene一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Scene一个详情信息", notes = "查询Scene一个详情信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
        @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Scene scene = sceneSV.load(id, code);
        log.info(JSON.toJSONString(scene));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, scene);
    }

    /**
     * 根据条件code查询Scene一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Scene一个详情信息", notes = "查询Scene一个详情信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isBlank(code)) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        Scene scene = sceneSV.loadByCode(code);
        log.info(JSON.toJSONString(scene));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, scene);
    }


    /**
     * 查询Scene信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Scene信息集合", notes = "查询Scene信息集合")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Scene scene, Integer curPage, Integer pageSize) {
        Page<Scene> page = new Page<Scene>(pageSize, curPage);
        List<Scene> scenes = sceneSV.list(scene, page.genRowStart(), page.getPageSize());
        page.setVoList(scenes);
        int total = sceneSV.count(scene);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, page);
    }


    /**
     * 查询Scene信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Scene信息集合", notes = "查询Scene信息集合")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
        @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<Scene> page = new Page<Scene>(pageSize, curPage);
        List<Scene> scenes = sceneSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = sceneSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, scenes);
    }

    /**
     * 统计Scene信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Scene信息数量", notes = "统计Scene信息数量")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
        @ApiImplicitParam(name = "num", value = "场景编号,比如供应商模块-添加场景01  最大不超过99 从00 开始", paramType = "query"),
        @ApiImplicitParam(name = "name", value = "场景名称", paramType = "query"),
        @ApiImplicitParam(name = "summary", value = "场景说明", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore Scene scene) {
        return sceneSV.count(scene);
    }


    /**
     * 创建场景
     * @return BeanRet
     */
    @ApiOperation(value = "创建场景", notes = "创建场景")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "num", value = "场景编号00~99,最大不超过99", paramType = "query", required =  true),
        @ApiImplicitParam(name = "name", value = "场景名称", paramType = "query", required =  true),
        @ApiImplicitParam(name = "summary", value = "场景说明", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Scene scene) {
        if(scene == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isBlank(scene.getNum())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isBlank(scene.getName())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        //场景编码是否存在
        Map<String, Object> params = new HashMap<>();
        params.put("num", scene.getNum());
        Scene sceneParam = sceneSV.load(params);
        if (sceneParam != null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Exists);
        }
        //场景名称是否存在
        params.clear();
        params.put("name", scene.getName());
        sceneParam = sceneSV.load(params);
        if(sceneParam != null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Exists);
        }
        sceneSV.save(scene);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, scene);
    }


    /**
     * 修改Scene
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Scene", notes = "修改Scene")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
        @ApiImplicitParam(name = "num", value = "场景编号00~99,最大不超过99", paramType = "query", required = true),
        @ApiImplicitParam(name = "name", value = "场景名称", paramType = "query", required = true),
        @ApiImplicitParam(name = "summary", value = "场景说明", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Scene scene) {
        if(scene == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(scene.getCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(scene.getNum())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        if(StringTools.isEmpty(scene.getName())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Empty_Param);
        }
        Scene sceneFlag = sceneSV.loadByNum(scene.getNum());
        if(sceneFlag == null) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        if(!sceneFlag.getCode().equals(scene.getCode())) {
            return BeanRet.create(false, BaseException.BaseExceptionEnum.Exists);
        }
        sceneSV.modify(scene);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, scene);
    }

}
